#include <fstream>
#include <iostream>
using namespace std;
// 按行读取
void readByLine(string fileName){
    fstream fr;
    fr.open(fileName, ios::in);
    if(fr){
        string line;
        while(getline(fr, line)){
            cout << line << endl;
        }
        cout << "finish reading";
    }else{
        cout << "no such file" << endl;
    }
}

// 创建文件并写入
void creatFile(string fileName){
    fstream fw;
    fw.open(fileName, ios::out | ios::trunc);
    if(fw){
        fw << "hello world" << endl;
        fw << "this is second line\n";
        fw << 1 << " " << 2.4;
    }else{
        cout << "fail to create file" << endl;
    }
}

int main(){
    creatFile("2.txt");
    return 0;
}